package metrics_sql

const (
	K8sContainerCpuUsage = `
guid = "%v"
from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "container_cpu_usage_seconds_total")
  |> filter(fn: (r) => r["namespace"] == guid )
  |> filter(fn: (r) => r["container"] != "POD" and r["container"]!="")
  |> filter(fn: (r) => r["pod"] != "jaeger-glue")
  |> filter(fn: (r) => r["container"] != "linkerd-proxy")
  |> filter(fn: (r) => r["id"] =~ /^\/kubepods.*/)
  |> difference()
  |> map(fn: (r) => ({_field:"cpu_seconds_total",_value:r._value / 30.0,_time:r._time,instance:r["container"],limit: 0}))
  |> window(every: inf)
`

	//	K8sContainerCpuUsage = `
	//query = (start=%v,stop=%v,guid="%v") => {
	//	cpu_seconds = from(bucket: "data")
	//	  |> range(start: start, stop: stop)
	//	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	//	  |> filter(fn: (r) => r["_field"] == "container_cpu_usage_seconds_total")
	//	  |> filter(fn: (r) => r["namespace"] == guid )
	//	  |> filter(fn: (r) => r["container"] != "POD" and r["container"]!="")
	//	  |> filter(fn: (r) => r["pod"] != "jaeger-glue")
	//	  |> filter(fn: (r) => r["id"] =~ /^\/kubepods.*/)
	//	  |> difference()
	//	  |> map(fn: (r) => ({_field:"cpu_seconds_total",_value:r._value,_time:r._time,instance:r["container"]}))
	//	  |> window(every: inf)
	//	cpu_limit =from(bucket: "data")
	//	  |> range(start: start, stop: stop)
	//	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	//	  |> filter(fn: (r) => r["_field"] == "kube_pod_container_resource_limits")
	//	  |> filter(fn: (r) => r["namespace"] == guid )
	//	  |> filter(fn: (r) => r["resource"] == "cpu")
	//	  |> last()
	//	  |> map(fn: (r) => ({_field:"cpu_limit",_value:r._value,_time:r._time,instance:r["container"]}))
	//	  |> window(every: inf)
	//	return join(tables: {cpu_seconds: cpu_seconds, cpu_limit: cpu_limit}, on: [ "_stop", "_start", "instance"])
	//		|> map(fn: (r) => ({_field:"cpu_usage",_value:r._value_cpu_seconds / 30.0 / r._value_cpu_limit  ,_time:r._time_cpu_seconds,instance:r["instance"],limit: 1}))
	//}
	//
	//query()
	//`
	K8sContainerCpuUsagelast = `
guid = "%v"
from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "container_cpu_usage_seconds_total")
  |> filter(fn: (r) => r["namespace"] == guid )
  |> filter(fn: (r) => r["container"] != "POD" and r["container"]!="")
  |> filter(fn: (r) => r["pod"] != "jaeger-glue")
  |> filter(fn: (r) => r["container"] != "linkerd-proxy")
  |> filter(fn: (r) => r["id"] =~ /^\/kubepods.*/)
  |> difference()
  |> last()
  |> map(fn: (r) => ({_field:"cpu_seconds_total",_value:r._value / 30.0,_time:r._time,instance:r["container"],limit: 0}))
  |> window(every: inf)
`
	GluenetsContainerCpuUsage = `
guid = "%v"
service = "%v"
name = guid+"_"+service

query = (start=%v,stop=%v,size=%f) => {
  return from(bucket: "data")
  |> range(start: start, stop: stop)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "container_cpu_usage_seconds_total")
  |> filter(fn: (r) => r["name"] == name)
  |> difference()
  |> group(columns:["_time"])
  |> sum(column: "_value")
  |> window(every: inf)
  |> map(fn: (r) => ({_field:"cpu_usage",_value:r._value  / 15.0  ,_time:r._time,instance:service,limit: 0.0}))
}

query()
`
	GluenetsContainerCpuUsagelast = `
guid = "%v"
service = "%v"
name = guid+"_"+service

query = (start=%v,stop=%v,size=%v) => {
  return from(bucket: "data")
  |> range(start: start, stop: stop)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "container_cpu_usage_seconds_total")
  |> filter(fn: (r) => r["name"] == name)
  |> difference()
  |> last()
  |> group(columns:["_time"])
  |> sum(column: "_value")
  |> window(every: inf)
  |> map(fn: (r) => ({_field:"cpu_usage",_value:r._value / 15.0  ,_time:r._time,instance:service,limit: 0.0}))
}

query()
`
)
